##--------------------------------------------------------------------------------##
##  Replication Code for: Zvobgo, Kelebogile, Wayne Sandholtz, and Suzie Mulesky. ##
##                        Reserving Rights: Examining Human Rights Treaty         ##
##                        Reservations." International Studies Quarterly.         ##
##                                                                                ##
##  *Figures 1-6 (main paper)                                                     ##
##  *Figure A4 (supplementary appendix)                                           ##
##                                                                                ##
##  Submitted for Public Posting in August 2020                                   ##
##--------------------------------------------------------------------------------##


# Set working directory
setwd("/Users/kelebogilezvobgo/Dropbox/Human Rights Treaty Reservations/_ISQ submission/ISQ_FINAL")


# Install and load packages
install.packages("foreign")
install.packages("readstata13")
install.packages("ggplot2")
install.packages("dplyr")
install.packages("tidyr")
install.packages("reshape2")
install.packages("ggrepel")
library(foreign)
library(readstata13)
library(ggplot2)
library(dplyr)
library(tidyr)
library(reshape2)
library(ggrepel)
source("http://peterhaschke.com/Code/multiplot.R")


# Read in the reservations data
data1 <- read.dta("Data/Treaty Obligations and Reservations zsm2020reserving.dta")
treatyid <- c("CAT", "CEDAW", "CERD", "CED", "CRC", "CRMW", "CRPD", "GENO", "ICCPR", "ICESCR")
treatydata <- data1[data1$treaty %in% treatyid,]

treatydata$treaty_name <- unlist(strsplit(treatydata$treaty, " "))[seq(2, 2*length(treatydata$treaty), by=2)]
table(treatydata$treaty_name)



#-------------------------------------#
# Figures
#-------------------------------------#

# Figure 1: Human Rights Treaty Reservations and Declarations, 1948-2014

res_scores <- aggregate(treatydata$reservation, list(treatydata$treaty), sum, na.rm = TRUE)
dec_scores <- aggregate(treatydata$declaration, list(treatydata$treaty), sum, na.rm = TRUE)

df1 <- data.frame(res_scores, dec_scores)
df2 <- subset(df1, select=c(Group.1, x, x.1))
df3 <- melt(df2, id= c("Group.1"))
head(df3)

p0 <- ggplot(df3, aes(x=Group.1, y=value, fill=variable)) +
  geom_bar(stat='identity', position='dodge') +
  xlab("Treaty") + ylab("Total Submitted") +
  theme(axis.title.x = element_text(size =20, face="bold")) +
  theme(axis.title.y = element_text(size =20, face="bold")) +
  scale_fill_manual(values=c("#181818", "#BFBFBF"), 
                    name="", #legend title
                    breaks=c("x", "x.1"),
                    labels=c("Reservations", "Declarations"))+
  theme(plot.title = element_text(size=20, face="bold", 
                                  margin = margin(10, 0, 10, 0))) +
  theme(axis.text.x = element_text(color="#181818", size=16, margin=margin(0,0,10,0), vjust=0.5))+
  theme(axis.text.y = element_text(color="#181818", size=16, margin=margin(0,0,0,10), vjust=0.5))+
  theme(legend.title=element_blank()) +
  theme(legend.text = element_text(size = 20))+
  theme(panel.border = element_blank(), panel.background = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "grey")) +
  scale_y_continuous()

setEPS()
postscript("Figures/Figure 1.eps", width=12, height=8)
p0
dev.off()



## Precision only

# reserv1 <- subset(treatydata, reservation==1)
# reserv2 <- subset(reserv1, !is.na(precise))
# reserv2$precise <- as.factor(reserv2$precise)
# 
# p1 <- ggplot(reserv2, aes(precise)) + geom_bar(aes(fill = precise), position = 'dodge') + 
#   facet_grid(treaty~., switch="both") + # group by treaty
#   xlab("Treaty") + ylab("Total Reservations") +
#   theme(axis.title.x = element_text(size =18, face="bold")) +
#   theme(axis.title.y = element_text(size =18, face="bold")) +
#   scale_fill_manual(values=c("#BFBFBF", "#181818"), 
#                     name="", #legend title
#                     breaks=c("0", "1"),
#                     labels=c("Imprecise", "Precise"))+
#   theme(plot.title = element_text(size=20, face="bold", 
#                                   margin = margin(10, 0, 10, 0))) +
#   theme(axis.text.y=element_blank(), axis.ticks.y = element_blank(), axis.line.y = element_blank()) + #
#   theme(axis.text.x=element_text(size=16, vjust=0.5)) +
#   theme(legend.title=element_blank()) +
#   theme(legend.text = element_text(size = 16)) +
#   theme(strip.background = element_blank(), strip.text.y = element_text(angle = 180, size=16)) + # format treaty labels
#   guides(fill = guide_legend(reverse=TRUE))+
#   theme(panel.border = element_blank(), panel.background = element_blank(), panel.grid.major = element_blank(),
#         panel.grid.minor = element_blank(), axis.line.x = element_line(colour = "grey")) +
#   scale_y_continuous(breaks = seq(0,300,50)) + coord_flip()
# p1



## Strength only

# reserv3 <- subset(reserv1, !is.na(strong))
# reserv3$strong <- as.factor(reserv3$strong)

# p2 <-ggplot(reserv3, aes(strong)) + geom_bar(aes(fill = strong), position = "dodge") +
#   facet_grid(treaty~., switch="both") + # group by treaty
#   xlab("Treaty") + ylab("Total Reservations") +
#   theme(axis.title.x = element_text(size =20, face="bold")) +
#   theme(axis.title.y = element_text(size =20, face="bold")) +
#   scale_fill_manual(values=c("#BFBFBF", "#181818"), 
#                     name="", #legend title
#                     breaks=c("0", "1"),
#                     labels=c("Weak", "Strong"))+
#   theme(plot.title = element_text(size=20, face="bold", 
#                                   margin = margin(10, 0, 10, 0))) +
#   theme(axis.text.y=element_blank(), axis.ticks.y = element_blank(), axis.line.y = element_blank()) + #
#   theme(axis.text.x=element_text(size=16, vjust=0.5)) +
#   theme(legend.title=element_blank()) +
#   theme(legend.text = element_text(size = 16)) +
#   theme(strip.background = element_blank(), strip.text.y = element_text(angle = 180, size=16)) + # format treaty labels
#   guides(fill = guide_legend(reverse=TRUE))+
#   theme(panel.border = element_blank(), panel.background = element_blank(), panel.grid.major = element_blank(),
#         panel.grid.minor = element_blank(), axis.line.x = element_line(colour = "grey")) +
#   scale_y_continuous(breaks = seq(0,300,50)) + coord_flip()
# p2



## Domestic Action only

# reserv4 <- subset(reserv1, !is.na(dom_action))
# reserv4$dom_action <- as.factor(reserv4$dom_action)

# p3 <- ggplot(reserv4, aes(dom_action)) + geom_bar(aes(fill = dom_action), position = "dodge") +
#   facet_grid(treaty~., switch="both") + # group by treaty
#   xlab("Treaty") + ylab("Total Reservations") +
#   theme(axis.title.x = element_text(size =20, face="bold")) +
#   theme(axis.title.y = element_text(size =20, face="bold")) +
#   scale_fill_manual(values=c("#BFBFBF", "#181818"), 
#                     name="", #legend title
#                     breaks=c("0", "1"),
#                     labels=c("No Domestic \nAction", "Domestic \nAction"))+
#   theme(plot.title = element_text(size=20, face="bold", 
#                                   margin = margin(10, 0, 10, 0))) +
#   theme(axis.text.y=element_blank(), axis.ticks.y = element_blank(), axis.line.y = element_blank()) + #
#   theme(axis.text.x=element_text(size=16, vjust=0.5)) +
#   theme(legend.title=element_blank()) +
#   theme(legend.text = element_text(size = 12)) +
#   theme(strip.background = element_blank(), strip.text.y = element_text(angle = 180, size=16)) + # format treaty labels
#   guides(fill = guide_legend(reverse=TRUE))+
#   theme(panel.border = element_blank(), panel.background = element_blank(), panel.grid.major = element_blank(),
#         panel.grid.minor = element_blank(), axis.line.x = element_line(colour = "grey")) +
#   scale_y_continuous(breaks = seq(0,300,50)) + coord_flip()
#p3



## Strength, Precision, and Domestic Action 

#multiplot(p2, p3, p1, cols=2)



# Figure A4: General Comments and Precision

gen_c <- subset(treatydata, gen_comment==1)
gen_com <- subset(gen_c, !is.na(precise)) #getting rid of missing values
gen_com$precise <- as.factor(gen_com$precise)

p4 <- ggplot(gen_com, aes(treaty)) + geom_bar(aes(fill = precise), position = "fill") +
  scale_alpha_discrete(range=c(0.4, 1)) +
  xlab("Treaty") + ylab("Obligations with General Comments") +
  theme(axis.title.x = element_text(size =20, face="bold")) +
  theme(axis.title.y = element_text(size =20, face="bold")) +
  scale_fill_manual(values=c("#BFBFBF", "#181818"), 
                    name="", #legend title
                    breaks=c("0", "1"),
                    labels=c("Imprecise", "Precise"))+
  theme(plot.title = element_text(size=20, face="bold", 
                                  margin = margin(10, 0, 10, 0))) +
  theme(axis.text.x = element_text(color="#181818", size=16, margin=margin(0,0,10,0), vjust=0.5))+
  theme(axis.text.y = element_text(color="#181818", size=16, margin=margin(0,0,0,10), vjust=0.5))+
  theme(legend.title=element_blank()) +
  theme(legend.text = element_text(size = 16)) +
  guides(fill = guide_legend(reverse=TRUE))+
  theme(panel.border = element_blank(), panel.background = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "grey")) +
  scale_y_continuous()

setEPS()
postscript("Figures/Figure A4.eps", width=12, height=8)
p4
dev.off()



# Read in the obligations data
data2 <- read.dta13("Data/Treaty Obligations zsm2020reserving.dta")
data2$obligation <- as.factor(data2$art_function == "5. obligation")


# Figure 2: Proportion of Provisions that Constitute Obligations

p5 <- ggplot(data2, aes(treaty)) + geom_bar(aes(fill = obligation), position = "stack") +
  xlab("Treaty") + ylab("Total Provisions") +
  theme(axis.title.x = element_text(size =20, face="bold")) +
  theme(axis.title.y = element_text(size =20, face="bold")) +
  scale_fill_manual(values=c("#181818", "#BFBFBF"), 
                    name="", #legend title
                    breaks=c("TRUE", "FALSE"),
                    labels=c("Obligations", "Other Provisions")) +
  theme(plot.title = element_text(size=20, face="bold", 
                                  margin = margin(10, 0, 10, 0))) +
  theme(axis.text.x = element_text(color="#181818", size=16, margin=margin(0,0,10,0), vjust=0.5))+
  theme(axis.text.y = element_text(color="#181818", size=16, margin=margin(0,0,0,10), vjust=0.5))+
  theme(legend.title=element_blank()) +
  theme(legend.text = element_text(size=12, vjust=0.5)) +
  theme(panel.border = element_blank(), panel.background = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "grey")) +
  scale_y_continuous(breaks = seq(0,300,50)) #+ coord_flip()

setEPS()
postscript("Figures/Figure 2.eps", width=12, height=8)
p5
dev.off()



# Figure 3: Proportion of Obligations that are Demanding

oblig <- data2[data2$obligation == "TRUE",]

p6 <- ggplot(oblig, aes(treaty)) + geom_bar(aes(fill = as.factor(demanding)), position = "stack") +
  xlab("Treaty") + ylab("Total Obligations") +
  theme(axis.title = element_text(size =20, face="bold")) +
  scale_fill_manual(values=c("#181818", "#BFBFBF"), 
                    name="", #legend title
                    breaks=c("1", "0"),
                    labels=c("Demanding", "Non-Demanding")) +
  theme(plot.title = element_text(size=20, face="bold", 
                                  margin = margin(10, 0, 10, 0))) +
  theme(axis.text.x = element_text(color="#181818", size=16, margin=margin(0,0,10,0), vjust=0.5))+
  theme(axis.text.y = element_text(color="#181818", size=16, margin=margin(0,0,0,10), vjust=0.5))+  
  theme(legend.title=element_blank()) +
  theme(legend.text = element_text(size=16, vjust=0.5)) +
  theme(panel.border = element_blank(), panel.background = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "grey")) +
  scale_y_continuous(breaks = seq(0,300,50)) #+ coord_flip()

setEPS()
postscript("Figures/Figure 3.eps", width=12, height=8)
p6
dev.off()



# Figure 4: Obligations with at least One Reservation

r <- aggregate(data1$reservation, list(data1$provision_id), sum)
df <- merge(r, oblig, by.y = "para_id", by.x = "Group.1", all.y = T)
df$x <- ifelse(is.na(df$x), FALSE, df$x > 0)

p7 <- ggplot(df, aes(treaty)) + geom_bar(aes(fill = as.factor(x)), position = "stack") +
  xlab("Treaty") + ylab("Total Obligations") +
  theme(axis.title = element_text(size =20, face="bold")) +
  scale_fill_manual(values=c("#181818", "#BFBFBF"), 
                    name="", #legend title
                    breaks=c("TRUE", "FALSE"),
                    labels=c("At least one \nreservation", "No \nreservations")) +
  theme(axis.text.x = element_text(color="#181818", size=16, margin=margin(0,0,10,0), vjust=0.5))+
  theme(axis.text.y = element_text(color="#181818", size=16, margin=margin(0,0,0,10), vjust=0.5))+  
  theme(legend.title=element_blank()) +
  theme(legend.text = element_text(size=12, vjust=0.5)) +
  theme(panel.border = element_blank(), panel.background = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "grey")) +
  scale_y_continuous(breaks = seq(0,300,50)) #+ coord_flip()

setEPS()
postscript("Figures/Figure 4.eps", width=12, height=8)
p7
dev.off()



# Figure 5: Demanding Obligations with at least One Reservation

p8 <- ggplot(df[df$demanding == 1,], aes(treaty)) + geom_bar(aes(fill = as.factor(x)), position = "stack") +
  xlab("Treaty") + ylab("Demanding Obligations") +
  theme(axis.title = element_text(size =20, face="bold")) +
  scale_fill_manual(values=c("#181818", "#BFBFBF"), 
                    name="", #legend title
                    breaks=c("TRUE", "FALSE"),
                    labels=c("At least one \nreservation", "No \nreservations")) +
  theme(axis.text.x = element_text(color="#181818", size=16, margin=margin(0,0,10,0), vjust=0.5))+
  theme(axis.text.y = element_text(color="#181818", size=16, margin=margin(0,0,0,10), vjust=0.5))+  
  theme(legend.title=element_blank()) +
  theme(legend.text = element_text(size=12, vjust=0.5)) +
  theme(panel.border = element_blank(), panel.background = element_blank(), panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "grey")) +
  scale_y_continuous(breaks = seq(0,300,50)) #+ coord_flip()

setEPS()
postscript("Figures/Figure 5.eps", width=12, height=8)
p8
dev.off()



# Figure 6: Mean Number of Reservations and Most Prominent Reserving States

r2 <- aggregate(data1$reservation, list(data1$treaty, data1$country), sum)
tmp <- r2 %>% group_by(Group.1) %>% top_n(n = 3, wt = x)
r2 <- merge(r2, tmp, by = c("Group.1", "Group.2"), all.x = T)
r2$labels <- ifelse(r2$x.y > 0, r2$Group.2, NA)

r2$labels[r2$labels == "United States of America"] <- "USA"
r2$labels[r2$labels == "United Kingdom of Great Britain and Northern Ireland"] <- "UK"
r2$labels[r2$labels == "Syrian Arab Republic"] <- "Syria"
r2$labels[r2$labels == "Venezuela (Bolivarian Republic of)"] <- "Venezuela"
r2$labels[r2$labels == "United Arab Emirates"] <- "UAE"

pos <- position_jitter(width = 0.5, seed = 5)
p9 <- ggplot(r2, aes(x = 0, y = x.x, label = labels)) + geom_point(aes(alpha = x.x), position = pos) + # boxplot of reservations by country
  facet_grid(~Group.1, switch = "x") +
  stat_summary(fun.y=mean, geom="point", shape=18, size=4) + # diamonds indicate mean number of reservations
  stat_summary(aes(label = sprintf("%.02f", ..y..)), fun.y=mean, geom="text", position = position_nudge(x = .25)) +
  geom_text_repel(aes(), box.padding = 0.25, min.segment.length = 1, point.padding = 0.4, position = pos) +
  xlab("Treaty") + ylab("Reservations by Country") + 
  theme(legend.position = "none") +
  theme(axis.text.x=element_blank(), axis.ticks.x = element_blank(), axis.line.x = element_blank()) + 
  theme(axis.text.y = element_text(color="#181818", size=16, margin=margin(0,0,0,10), vjust=0.5))+
  theme(axis.title = element_text(size =20, face="bold")) +
  theme(panel.border = element_rect(color = "grey",fill = NA), panel.background = element_blank(),
        axis.line = element_line(colour = "grey")) 

jpeg("Figures/Figure 6.jpeg", res = 300, width = 4200, height = 2550,  quality = 100)
p9
dev.off()